using System;
using System.Collections.Generic;
using TestCases.Utils;

namespace TestCases.CompareAssert.Matchers
{
    
    public class InstanceOfMatcher: IObjectMatcher<Type>
    {
        private readonly HashSet<Type> _types = new HashSet<Type>();
                       
        public InstanceOfMatcher(Type type)
        {
            _types.Add(type);
        }
        
        public InstanceOfMatcher(IEnumerable<Type> types)
        {
            _types.AddAll(types);
        }
        
        public bool IsMatch(Type value)
        {
            foreach (var type in _types)
            {
                if (type.IsAssignableFrom(value))
                {
                    return true;
                }
            }
            return false;
        }

        public bool Equals(InstanceOfMatcher other)
        {
            return (other != null && (other == this || _types.SetEquals(other._types)));
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as InstanceOfMatcher);
        }

        public override int GetHashCode()
        {
            return _types.GetHashCode();
        }
    }
}